60B - Serial Time - CodeForces Solution


dfs and similar dsu *1400

Please click on ads to support us..

Python Code:

z, n, m = list(map(int, input().split()))

N = 12
adj = [[['' for _ in range(N)] for _ in range(N)] for _ in range(N)]
visited = [[[False for _ in range(N)] for _ in range(N)] for _ in range(N)]

for i in range(1, z+1):
  input()
  for j in range(1, n+1):
    chars = input()
    for k in range(1, m+1):
      adj[i][j][k] = chars[k-1]

input()
x, y = list(map(int, input().split()))

dx = [ 1,-1,0,0,0,0 ]
dy = [ 0,0,1,-1,0,0 ]
dz = [ 0,0,0,0,1,-1 ]

def dfs(l, x, y):
  if adj[l][x][y] == '#':
    return
  
  visited[l][x][y] = True
  for i in range(6):
    a = x + dx[i]
    b = y + dy[i]
    c = l + dz[i]

    if a > 0 and b > 0 and c > 0 and c <= z and b <= m and a <= n and not visited[c][a][b]:
      dfs(c, a, b)

dfs(1, x, y)

ans = 0
for i in range(1, z+1):
  for j in range(1, n+1):
    for k in range(1, m+1):
      if visited[i][j][k]:
        ans += 1

print(ans)

C++ Code:

#include<bits/stdc++.h> 
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rep2(i,j,a,b) rep(i,0,a-1)rep(j,0,b-1)
#define rep3(i,j,k,a,b,c) rep2(i,j,a,b)rep(k,0,c-1)
int a,b,c,m,n,ans;
int vis[15][15][15];
char s[15][15][15];
int dx[]={-1,1,0,0,0,0};
int dy[]={0,0,-1,1,0,0};
int dz[]={0,0,0,0,-1,1};
struct M{int x,y,z;}p[15][15][15];
queue<M>q;
bool check(int x,int y,int z){return x>=0&y>=0&z>=0&x<a&y<b&z<c;}
int main(){
	cin>>a>>b>>c;
	rep2(i,j,a,b)cin>>s[i][j];
	cin>>m>>n;
	q.push({0,m-1,n-1});
	while(!q.empty()){
		M k=q.front();
//		cout<<k.x<<k.y<<k.z<<endl;
		q.pop();
		vis[k.x][k.y][k.z]=1;
		rep(i,0,5)
			if(check(k.x+dx[i],k.y+dy[i],k.z+dz[i]))
				if(s[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]]=='.')
					if(!vis[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]])
						q.push({k.x+dx[i],k.y+dy[i],k.z+dz[i]}),
						vis[k.x+dx[i]][k.y+dy[i]][k.z+dz[i]]=1;
	}
	rep3(i,j,k,a,b,c)ans+=vis[i][j][k];
	cout<<ans;
	return 0;
}
			 	  	 	  	     				 					 	


Comments

Submit
0 Comments
More Questions

501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND
1669E - 2-Letter Strings
483A - Counterexample
3C - Tic-tac-toe
1669F - Eating Candies
1323B - Count Subrectangles
991C - Candies
1463A - Dungeon
1671D - Insert a Progression
1671A - String Building
1671B - Consecutive Points Segment
1671C - Dolce Vita
1669G - Fall Down
4D - Mysterious Present
1316B - String Modification
1204A - BowWow and the Timetable
508B - Anton and currency you all know
1672A - Log Chopping
300A - Array
48D - Permutations